package com.yunquan.ohana.ui.history.status.adapter

import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.yunquan.ohana.R
import com.yunquan.ohana.entity.GroupFramesEntity

class StatusAdapter(private val data: List<GroupFramesEntity>) : RecyclerView.Adapter<StatusAdapter.ItemViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        val view = View.inflate(parent.context, R.layout.item_receiving_status, null)
        return ItemViewHolder(view)
    }

    override fun getItemCount(): Int {
        return data.size
    }

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        val item = data[position]
        holder.bind(item)
    }

    fun isGroupHeader(position: Int): Boolean {
        if (position == 0) {
            return true
        } else if (position >= data.size) {
            return false
        } else {
            val last = data[position - 1].receipt
            val current = data[position].receipt
            return last != current
        }
    }

    fun getItemData(position: Int): GroupFramesEntity {
        return data[position]
    }

    inner class ItemViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
        private val tvName = view.findViewById<TextView>(R.id.tv_frames_name)
        private val tvLocation = view.findViewById<TextView>(R.id.tv_frames_location)

        fun bind(item: GroupFramesEntity) {
            tvName.text = item.name
            tvLocation.text = item.roomLocation
        }

    }
}